﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DevComponents.DotNetBar.Controls;
using System.Data;

namespace BUL
{
    public partial class Business
    {      
        public void GetAllMauSac(string strTimkiem, DataGridViewX dataGridViewX )
        {
            if(dataGridViewX.RowCount>0)
                dataGridViewX.Rows.Clear();
            using (var dal = new QuanLyBanHangEntities1())
            {
                var listResult = dal.tblMauSacs.Where(x => x.TenMauSac.Contains(strTimkiem)).OrderBy(x => x.TenMauSac);
                int i = 0;
                foreach (var item in listResult)
                {
                    dataGridViewX.Rows.Add();
                    dataGridViewX.Rows[i].Cells[0].Value = "N";
                    dataGridViewX.Rows[i].Cells[1].Value = item.MaMauSac;
                    dataGridViewX.Rows[i].Cells[2].Value = item.TenMauSac;
                    dataGridViewX.Rows[i].Cells[3].Value = item.GhiChu;
                    i++;
                }
            }
        }

        public tblMauSac GetMauSacById(string mamausac)
        {
            return _dal.tblMauSacs.Where(x => x.MaMauSac == mamausac).FirstOrDefault();
        }

        public void InsertMauSac(tblMauSac mausac)
        {
            _dal.tblMauSacs.AddObject(mausac);
            _dal.SaveChanges();
        }

        public void UpdateMauSac(string maMauSacCu, tblMauSac mausac)
        {
            var MSUpdate = _dal.tblMauSacs.Where(x => x.MaMauSac == maMauSacCu).FirstOrDefault();
            MSUpdate.MaMauSac = mausac.MaMauSac;
            MSUpdate.TenMauSac = mausac.TenMauSac;
            MSUpdate.GhiChu = mausac.GhiChu;
            _dal.SaveChanges();
        }

        public void DeleteMS(IEnumerable<string> mamausacs)
        {
            var listDelete = _dal.tblMauSacs.Where(x => mamausacs.Contains(x.MaMauSac));
            foreach (var tblmausac in listDelete)
            {
                _dal.tblMauSacs.DeleteObject(tblmausac);
            }
            _dal.SaveChanges();
        }

        public tblMauSac BuildMS(string maMS, string tenMS, string ghichu)
        {
            return new tblMauSac { MaMauSac = maMS, TenMauSac = tenMS,GhiChu = ghichu };
        }
    }
}
